package com.frejoys.service.huajuan.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frejoys.common.enums.EStatus;
import com.frejoys.common.util.SnowflakeUtil;
import com.frejoys.common.util.TimeUtil;
import com.frejoys.dao.entity.huajuan.HuajuanScore;
import com.frejoys.dao.mapper.huajuan.HuajuanScoreMapper;
import com.frejoys.dao.mapper.huajuan.HuajuanUserMapper;
import com.frejoys.service.huajuan.HuajuanScoreService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class HuajuanScoreServiceImpl extends ServiceImpl<HuajuanScoreMapper, HuajuanScore> implements HuajuanScoreService {

    private final HuajuanUserMapper huajuanUserMapper;


    @Override
    public boolean addIncrScoure(Integer huajuanUserId, Integer score) {
        return save(new HuajuanScore(){{
            setHuajuanUserId(huajuanUserId);
            setMsgSn(SnowflakeUtil.appNextId());
            setScore(score);
            setCreateTime(TimeUtil.unixTime());
            setUpdateTime(TimeUtil.unixTime());
            setType(1);
            setStatus(0);
        }});
    }

    @Override
    public boolean addDecrScoure(Integer huajuanUserId, Integer score) {
        return save(new HuajuanScore(){{
            setHuajuanUserId(huajuanUserId);
            setMsgSn(SnowflakeUtil.appNextId());
            setScore(score);
            setCreateTime(TimeUtil.unixTime());
            setUpdateTime(TimeUtil.unixTime());
            setType(2);
            setStatus(0);
        }});
    }

    @Override
    public boolean upScoure(Long msgSn, Integer status) {
        LambdaUpdateWrapper<HuajuanScore> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(HuajuanScore::getStatus, status);
        wrapper.eq(HuajuanScore::getMsgSn, msgSn);
        return update(wrapper);
    }

    @Override
    public List<HuajuanScore> getWaitList(Integer page, Integer size) {
        Page p = new Page<>(page, size);
        p.setSearchCount(false);

        LambdaQueryWrapper<HuajuanScore> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(HuajuanScore::getHuajuanUserId, HuajuanScore::getMsgSn, HuajuanScore::getScore, HuajuanScore::getType).eq(HuajuanScore::getStatus, EStatus.off.getValue());

        return baseMapper.selectPage(p, wrapper).getRecords();
    }

    @Override
    public boolean setUserScore(Long userId, Integer score, Integer type) {
        Integer huajuanUserId = huajuanUserMapper.getHuajuanUserIdInteger(userId);
        if (huajuanUserId != null) {
            if (type.equals(1)) {
                return addIncrScoure(huajuanUserId, score);
            } else if (type.equals(2)) {
                return addDecrScoure(huajuanUserId, score);
            }
        }

        return false;
    }

}
